// Copyright 2022 a.o.smith ALL Rights Reserved
package com.liuxinlong.modules.chimi.service.impl;

import com.liuxinlong.common.AosException;
import com.liuxinlong.common.SnowFlake;
import com.liuxinlong.enums.ReturnCodes;
import com.liuxinlong.modules.chimi.service.BarPointsConfigService;
import com.liuxinlong.modules.dao.BarCardDao;
import com.liuxinlong.modules.dao.BarPointsConfigDao;
import com.liuxinlong.modules.entity.BarPointsConfig;
import com.liuxinlong.utils.ObjectUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 论坛积分配置服务层接口实现
 *
 * @author liuxinlong@hotwater.com.cn
 * @version 1.0.0
 * @since 2023-02-13
 */
@Service
@Slf4j
public class BarPointsConfigServiceImpl implements BarPointsConfigService {

    @Autowired
    private BarPointsConfigDao barPointsConfigDao;

    @Autowired
    private BarCardDao barCardDao;

    @Override
    public List<Map<String, Object>> pagePointsConfig(Map<String, Object> queryParam) {
        int startNum = (int) queryParam.get("startNum");
        List<BarPointsConfig> originalList = barPointsConfigDao.pageBarPointsConfig(queryParam);
        return completeResult(originalList,startNum);
    }

    @Override
    public int getPointsConfigCount(Map<String, Object> queryParam) {
        return barPointsConfigDao.getBarPointsConfigCount(queryParam);
    }

    @Override
    public void addPointsConfig(BarPointsConfig barPointsConfig) {
        barPointsConfig.setId(SnowFlake.nextIdStr());
        barPointsConfigDao.insert(barPointsConfig);
    }

    @Override
    public void updatePointsConfig(BarPointsConfig barPointsConfig) {
        String id = barPointsConfig.getId();
        if (StringUtils.isEmpty(id)) {
            throw new AosException(ReturnCodes.HTTP_PARAM_ERROR,"修改积分配置信息异常，配置不存在！");
        }
        BarPointsConfig oldInfo = barPointsConfigDao.selectById(id);
        if (ObjectUtils.isEmpty(oldInfo)) {
            throw new AosException(ReturnCodes.HTTP_PARAM_ERROR,"修改积分配置信息异常，配置不存在！");
        }
        barPointsConfigDao.updateById(barPointsConfig);
    }

    @Override
    public void deletePointsConfig(String id) {
        if (StringUtils.isEmpty(id)) {
            throw new AosException(ReturnCodes.HTTP_PARAM_ERROR,"删除积分配置信息异常，配置不存在！");
        }
        BarPointsConfig oldInfo = barPointsConfigDao.selectById(id);
        if (ObjectUtils.isEmpty(oldInfo)) {
            throw new AosException(ReturnCodes.HTTP_PARAM_ERROR,"删除积分配置信息异常，配置不存在！");
        }
        barPointsConfigDao.deleteById(id);
    }

    /**
     * 数据补全转换
     * @param originList 原始数据列表
     * @param startNum 起始查询数
     * @return 补全数据
     */
    private List<Map<String, Object>> completeResult(List<BarPointsConfig> originList, int startNum) {
        if (ObjectUtils.isEmpty(originList)) {
            return new ArrayList<>();
        }
        List<Map<String, Object>> resultList = new ArrayList<>(originList.size());
        int sort = startNum + 1;
        for (BarPointsConfig record : originList) {
            Map<String, Object> map = ObjectUtils.object2Map(record);
            map.put("sort", sort);
            resultList.add(map);
            sort++;
        }
        return resultList;
    }
}
